package com.spa.infrastructure.util;

import cn.hutool.core.util.ObjectUtil;
import com.spa.domain.entity.CitiesServed;
import com.spa.domain.entity.Shop;
import com.spa.domain.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Component
public class FeeUtil {

    //起步里程
    public static final Long DEFAULT_DISTANCE = 3L;
    //起步价
    public static final Double STARTING_PRICE = 800D;
    //每公里加价
    public static final Double INCREMENT_PRICE = 180D;
    //路费 来回计价
    private static final Double DOUBLE = 2D;

    @Autowired
    private ShopService shopService;

    /**
     * 计算往返路费
     * @return 路费
     */
    public double calculateFee(BigDecimal ret, long shopId) {
        //先通过门店ID找到所在城市
        Shop shop = shopService.getShopById(shopId);
        CitiesServed cities = shop.getCity();
        double basePrice = ObjectUtil.isNotNull(cities.getPrice()) && cities.getPrice()!=0 ? cities.getPrice() : STARTING_PRICE;
        double price = basePrice;
        double distance = ObjectUtil.isNotNull(cities.getDistance()) && cities.getDistance()!=0 ? cities.getDistance() : DEFAULT_DISTANCE;
        double incrementPrice = ObjectUtil.isNotNull(cities.getIncrementPrice()) && cities.getIncrementPrice()!=0 ? cities.getIncrementPrice() : INCREMENT_PRICE;
        if (ret.doubleValue() - distance > 0) {
            price = basePrice + (Math.ceil(ret.doubleValue()) - distance ) * incrementPrice;
        }
        double total = price * DOUBLE;
//        if (couponSwitch) {
//            if (isUseCoupon) {
//                total = total - basePrice * 2;
//            }
//            if (total <= 0) {
//                total = 1;
//            }
//        }
        return total;
    }

}
